
class Maprail{
	typelist = ['Circle','Rectangle','Polygon']
	style = {
		strokeColor:'#E94837',
		fillColor:'#E94837',
		fillOpacity:0.2
	}
	constructor({map,type,data,label}){
		this.map = map;
		this.typenum = type;
		this.type = this.typelist[type]; //0圆形  1矩形  2多边形
		this.data = data;
		this._obj = null;
		this._objGroup = new  AMap.OverlayGroup();
		this.label = label;
		this.obj = null;
		this._init();
	}

	_init(){
		if(this.data){
			let opts = {map:this.map,bubble:true,...this.style};
			if(this.typenum === 0){
				opts.center = new AMap.LngLat(this.data[0].lng,this.data[0].lat);
				opts.radius = this.data[0].radius;
			}else if(this.typenum === 2){
				opts.path = this.data.map(v=>new AMap.LngLat(v.lng,v.lat));
			}else{
				let path = [];
				path.push(new AMap.LngLat(this.data[1].lng,this.data[0].lat));
				path.push(new AMap.LngLat(this.data[0].lng,this.data[0].lat));
				path.push(new AMap.LngLat(this.data[0].lng,this.data[1].lat));
				path.push(new AMap.LngLat(this.data[1].lng,this.data[1].lat));
				opts.path = path;
			}
			if(this.typenum === 1){
				this._obj = new AMap[`Polygon`](opts);
			}else{
				this._obj = new AMap[`${this.type}`](opts);
			}
			
			if(this.label){
				this._labelObj = new AMap.Text({
					map:this.map,
					text:this.label,
					position:this._obj.getBounds().getCenter()
				});
				this._objGroup.addOverlay(this._labelObj);
			}
			this._objGroup.addOverlay(this._obj);
		}else{
			this.mouseTool = new AMap.MouseTool(this.map);
			this.mouseTool.on("draw",({type,obj})=>{
				this.mouseTool.close(false);
				this._setObj(obj);
				this.editorTool = this._createEditorTool(obj,true);
				this.editorTool.open();
			})
		}
	}

	_eventSetObj = ({type,target:obj})=>{
		this._setObj(obj);
	}

	_setObj = obj=>{
		this.obj = obj;
	}

	_bindEditorEvent(tool){
		tool.on("adjust",this._eventSetObj)
		tool.on("end",this._eventSetObj)

		if(this.typenum === 2){
			tool.on("addnode",this._eventSetObj)
			tool.on("removenode",this._eventSetObj)
		}else if(this.typenum === 0){
			tool.on("move",this._eventSetObj);
		}
	}

	_createEditorTool(obj,flag){
		if(this.typenum === 1&&flag){
			let _obj = obj;
			obj = new AMap.Rectangle({..._obj.getOptions(),bounds:_obj.getBounds()});
			this._setObj(obj);
			this.map.remove(_obj);
		}

		let s = this.typenum ===2 ?
			new AMap[`PolyEditor`](this.map,obj)
			:new AMap[`${this.type}Editor`](this.map,obj);

		this._bindEditorEvent(s);
		return s;
	}

	remove(flag){
		if(this.obj)
			this.map.remove(this.obj);
		if(this.editorTool)
			this.editorTool.close(true);
		if(this.mouseTool)
			this.mouseTool.close(true);
		if(flag&&this._obj){
			this.map.remove(this._obj);
			this._obj = null;
		}
		if(flag&&this._labelObj){
			this.map.remove(this._labelObj);
		}
		this.obj = null;
	}

	getFormData(){
		let data = null;
		if(this.obj){
			data = [];
			if(this.typenum === 0){
				data.push({
					lat:this.obj.getCenter().lat,
					lng:this.obj.getCenter().lng,
					radius:this.obj.getRadius(),
					sort:1
				})
			}else if(this.typenum === 1){
				let b = this.obj.getBounds();
				data.push({
					lat:b.getSouthWest().lat,
					lng:b.getSouthWest().lng,
					sort:1
				});
				data.push({
					lat:b.getNorthEast().lat,
					lng:b.getNorthEast().lng,
					sort:2
				});
			}else{
				data = this.obj.getPath().map((v,i)=>({lat:v.lat,lng:v.lng,sort:i+1}))
			}
		}
		return data;
	}

	reset(){
		if(this._obj){
			this._objGroup.show();
		}
		this.remove();
	}
	//绘制围栏 只有当围栏没有围栏数据的时候才能create
	create(){
		!this.data?this.mouseTool[this.type.toLowerCase()]():new Error('已有围栏数据不允许重复创建');
	}

	center(){
			this.map.setFitView(this._obj);
	}
	//修改围栏 只有拥有围栏数据的时候才能eidt
	edit(){
		let opts = {map:this.map,...this.style};
		if(this.typenum === 0){
			opts.center = new AMap.LngLat(this.data[0].lng,this.data[0].lat);
			opts.radius = this.data[0].radius;
		}else if(this.typenum === 2){
			opts.path = this.data.map(v=>new AMap.LngLat(v.lng,v.lat));
		}else{
			opts.bounds = new AMap.Bounds(...this.data.map(v=>[v.lng,v.lat]));
		}
		this.obj = new AMap[`${this.type}`](opts);
		this._objGroup.hide();
		this.editorTool = this._createEditorTool(this.obj);
		this.editorTool.open();
	}
}
export default Maprail;